<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/9/13 0013
  Time: 20:12
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page pageEncoding="utf-8" contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>用户管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/layui.css"  media="all">
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>

<table class="layui-hide" id="table" lay-filter="table">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">用户管理/用户管理</div>
                <div class="layui-card-body">
                    <form class="layui-form" action="" lay-filter="example">

                        <div class="layui-form-item">
                            <div class="layui-inline">
                                <label class="layui-form-label">用户名</label>
                                <div class="layui-input-inline">
                                    <input type="text" id="username" name="username" placeholder="请输入用户名称" autocomplete="off" class="layui-input">
                                </div>
                            </div>



                            <div class="layui-inline">
                                <button type="button" class="layui-btn layuiadmin-btn-list" id="btn-search">
                                    <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                                </button>
                            </div>
                        </div>



                    </form>

                </div>
            </div>
        </div>
    </div>







</table>



<script type="text/html" id="toolbar">


    <div class="layui-btn-container">

        <button class="layui-btn" lay-event="save">新增</button>
        <button class="layui-btn" lay-event="remove">删除</button>
        <button class="layui-btn" lay-event="update">编辑</button>

    </div>
</script>

<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>




<script src="https://cdn.bootcss.com/jquery/2.2.3/jquery.js"></script>
<script src="${pageContext.request.contextPath}/static/layui.js" charset="utf-8"></script>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->

<script>

    layui.use(['form','table','jquery','dropdown','form'], function(){
        var table = layui.table;
        var $=layui.jquery;
        var form=layui.form;
        var dropdown = layui.dropdown;
        table.render({
            elem: '#table'
            ,url:'${pageContext.request.contextPath}/admin/user/queryByPage'
            ,toolbar: '#toolbar' //开启头部工具栏，并为其绑定左侧模板
            ,method:'post'
            ,defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
                title: '提示'
                ,layEvent: 'LAYTABLE_TIPS'
                ,icon: 'layui-icon-tips'
            }]
            ,title: '用户列表'
            ,cols: [[
                {type: 'checkbox', fixed: 'left'}
                ,{field:'id', title:'用户ID',hide:true, width:120, fixed: 'left', unresize: true, sort: true}
                ,{field:'username', title:'用户名称', width:120}
                ,{field:'phone', title:'电话号码'}
                ,{field:'email', title:'电子邮件'}
            ]]
            ,page: true
        });




        $('#btn-search').on('click',function() {
            var username = $("#username").val();
            table.reload('table', {
                page: {
                    curr: 1 //重新从第 1 页开始
                }
                ,where: {
                    'username':username
                }
            });
        });

        //头工具栏事件
        table.on('toolbar(table)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'save':
                    $("#addForm")[0].reset();
                    form.render();
                    $("#id").val("0");
                    layer.open({
                        type: 1,
                        shade: false,
                        title: false, //不显示标题
                        area: ['620px', '380px'], //宽高
                        content: $('#addPanel'), //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
                        cancel: function(){
                            $(".layui-laypage-btn").click();//执行分页刷新当前页
                        }
                    });
                    break;
                case 'remove':
                    var data = checkStatus.data;
                    if(data.length!=1)
                    {
                        layer.alert('至少选择一条记录进行删除');
                        return false;
                    }
                    var id=data[0]['id'];
                    layer.confirm('你确定要删除么', function(index) {
                        var id = data[0]['id'];
                        $.get("/admin/user/remove/" + id, {}, function (data) {
                            if(data.code==0)
                            {
                                layer.close(index);
                                layer.msg(data.msg);
                                //把表格给刷新一下
                                table.reload('table', {
                                    page: {
                                        curr: 1 //重新从第 1 页开始
                                    }
                                });

                            }
                        }, 'json');
                    });

                    break;
                case 'update':
                    var data = checkStatus.data;
                    if(data.length!=1)
                    {
                        layer.alert('只能选择一条记录进行修改');
                        return false;
                    }
                    var id=data[0]['id'];
                    $.get("/admin/user/querybyid/"+id,{},function(data){
                        if(data.code==0)
                        {
                            const obj=data.data[0];
                            form.val("example",obj);
                            layer.open({
                                type: 1,
                                shade: false,
                                title: false, //不显示标题
                                area: ['620px', '380px'], //宽高
                                content: $('#addPanel'), //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
                                cancel: function(){
                                    $(".layui-laypage-btn").click();//执行分页刷新当前页
                                }
                            });
                        }

                    },'json');
                    break;
                //自定义头工具栏右侧图标 - 提示
                case 'LAYTABLE_TIPS':
                    layer.alert('这是工具栏右侧自定义的一个图标按钮');
                    break;
            };
        });




        //数据校验
        form.verify();

        //监听提交
        //监听提交
        form.on('submit(demo1)', function(data){
            const obj=data.field;
            const id=obj.id;
            const method=id==0?'save':'update';
            $.post("/admin/user/"+method,data.field,function (data){
                if(data.code==0)
                {
                    //把表格给刷新一下
                    table.reload('table', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                    });
                    //关闭我们的弹出的框
                    layer.closeAll();
                    layer.msg(data.msg);
                }
            },"json")
            return false;
        });


    });





</script>

</body>



<div id="addPanel"  style="padding: 10px; background-color: #F2F2F2;display:none">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header"></div>
                <div class="layui-card-body">
                    <form class="layui-form" id="addForm" action="" lay-filter="example">
                        <input type="hidden" name="id" id="id" value="0">
                        <div class="layui-form-item">
                            <label class="layui-form-label">用户名</label>
                            <div class="layui-input-block">
                                <input type="text" name="username" lay-verify="required" autocomplete="off" placeholder="请输入分类名称" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">密码</label>
                            <div class="layui-input-block">
                                <input type="password" name="password" lay-verify="required" autocomplete="off" placeholder="请输入分类名称" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <label class="layui-form-label">电话号码</label>
                            <div class="layui-input-block">
                                <input type="phone" name="phone" lay-verify="required" autocomplete="off" placeholder="请输入分类名称" class="layui-input">
                            </div>
                        </div>


                        <div class="layui-form-item">
                            <label class="layui-form-label">电子邮箱</label>
                            <div class="layui-input-block">
                                <input type="email" name="email" lay-verify="required" autocomplete="off" placeholder="请输入分类名称" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-form-item">
                            <div class="layui-input-block">

                                <button type="submit" class="layui-btn" lay-submit="" lay-filter="demo1">提交</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>

    </div>
</div>

</html>

